package comu.hnevc.zh.testdemo.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import comu.hnevc.zh.testdemo.dialog.AccountDialog;
import comu.hnevc.zh.testdemo.bean.Car;
import comu.hnevc.zh.testdemo.R;

public class AccountAdapter extends BaseAdapter {
    private Context mContext;
    List<Car> mCarList=new ArrayList<>();

    public void setDataCars(List<Car> mCarList){
        this.mCarList=mCarList;
        notifyDataSetChanged();
    }

    public AccountAdapter(Context mContext){
        this.mContext=mContext;
    }
    @Override
    public int getCount() {
        return mCarList.size();
    }

    @Override
    public Object getItem(int position) {
        return mCarList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        Log.e("position",position+"");
        final  Car car=mCarList.get(position);
        convertView=LayoutInflater.from(mContext).inflate(R.layout.item_account_list,null);
        TextView carid=convertView.findViewById(R.id.tv_accoun_carid);
        TextView carnum=convertView.findViewById(R.id.tv_accoun_carnum);
        TextView caruser=convertView.findViewById(R.id.tv_accoun_caruser);
        TextView carbalnce=convertView.findViewById(R.id.tv_accoun_carblance);
        ImageView iv=convertView.findViewById(R.id.iv_accoun_cardrow);
        CheckBox cb=convertView.findViewById(R.id.cb_accoun);
        Button bt=convertView.findViewById(R.id.bt_accoun);

        carid.setText(car.getCarid()+"");
        carnum.setText(car.getCarnum());
        caruser.setText(car.getCaruser());
        carbalnce.setText(car.getCarblance());
        iv.setBackgroundResource(car.getCardrow());
        cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                car.setNeedchaerge(isChecked);
            }
        });
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<Car> mCarListSelect=new ArrayList<>();
                mCarListSelect.clear();
                for (int i=0;i<mCarList.size();i++){
                    if (mCarList.get(position).isNeedchaerge()==true){}
                    mCarListSelect.add(mCarList.get(position));
                    break;
                }
                  if (mCarListSelect.size()<=0){
                      Toast.makeText(mContext,"未选中车辆",Toast.LENGTH_SHORT).show();
                      return;
                  }
                  AccountDialog dialog=new AccountDialog(mContext,mCarListSelect);
                dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
                dialog.show();
            }

        });
        return convertView;
    }
     public  void updateCarList(){
        for(int i=0;i<mCarList.size();i++){
            mCarList.get(i).setNeedchaerge(false);
        }

     }
}
